package com.atguigu03.threadsafe.threadsafe;

/**
 * ClassName: WindowTest1
 * Package: com.atguigu03.threadsafe.notsafe
 * Description:通过继承Thread进行售票练习,使用同步代码块的方式解决线程安全问题
 *
 * @Author ZY
 * @Create 15:45
 * @Version 1.0
 */


class Window extends Thread{

    static int ticket = 100;
    static Object object = new Object();
    @Override
    public void run() {

        while(true){

//            synchronized(this){//this:此时表示w1、w2、w3三个对象
//            synchronized(object){//object:使用static修饰时，就能保证其唯一性。
            synchronized(Window.class){
                if (ticket > 0){

                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    System.out.println(Thread.currentThread().getName() + "售票，票号为：" + ticket);
                    ticket--;
                }else {
                    break;
                }
            }

        }

    }
}


public class WindowTest1 {
    public static void main(String[] args) {

        Window w1 = new Window();
        Window w2 = new Window();
        Window w3 = new Window();

        w1.setName("窗口1");
        w2.setName("窗口2");
        w3.setName("窗口3");

        w1.start();
        w2.start();
        w3.start();
    }
}
